<?php
namespace Home\Controller;
use Common\Controller\HomeBaseController;
use Think\Verify;
use Common\Model\MemberModel;
use Think\Model;
use Common\Model\RenwuModel;
use Common\Model\TixianModel;
use Common\Model\ChongzhiModel;
use Common\Model\ArticleModel;
use Common\Model\JiluModel;
use Common\Model\DianpuModel;

class ShangController extends HomeBaseController {
	protected function _initialize() {
		parent::_initialize();
		if (! MID) { // 还没登录 跳转到登录页面
			if (IS_POST) $this->error('登录超时, 请重新登录');
			else $this->redirect(C('LOGIN_URL'));
		}
		$user = session('user_auth');//判断用户是否进行短信验证
		$member = new MemberModel();
		if($member->where("id=".MID)->getField("status") == -1){
			$this->redirect("Index/duanxin");
		}
	}
	/*
	 * 商家任务大厅
	*/
	public function lists($cate = NULL,$xinyong = NULL){
		$renwu = new RenwuModel();
		$member = new MemberModel();
		$map = array();
		$map['status'] = array("eq",1);
		/************筛选*****************/
		if(!empty($cate)){
			$map['cate'] = $cate;
			$this->assign("cate",$cate);
		}
		$filter = I('post.');
		if($filter['xinyong']){
			$map['xinyong'] = $filter['xinyong'];
			$this->assign("xinyong",$filter['xinyong']);
		}
		if($filter['orderid']){
			$map['id'] = $filter['orderid'];
			$this->assign("orderid",$filter['orderid']);
		}
		/************筛选*****************/		
		C('LIST_ROWS',C('cfg_linerows'));
		$list = $this->_lists($renwu,$map,'createtime DESC');
		//list数据处理
		foreach ($list as &$v) {
			$v['time_type'] =date('Y-m-d H:i:s',$v['createtime']);
			$v['faburen'] = $member->where("id=".$v['sid'])->getField("account");
		}
		unset($v);
		$this->assign("list",$list);
		$this->display();
	}
	/*
	 * 任务详情
	*/
	public function info($id) {
		$renwu = new RenwuModel();
		$member = new MemberModel();
		$info = $renwu->where("id = ".$id)->find();
		if($info['cate'] == 1){$info['renwu_type'] = "垫付";	}
		elseif ($info['cate'] == 2){$info['renwu_type'] = "立返";	}
		elseif ($info['cate'] == 3){$info['renwu_type'] = "远程";	}
		elseif ($info['cate'] == 4){$info['renwu_type'] = "浏览";	}
		$info['time_type'] = date('Y-m-d H:i:s',$info['createtime']);
		if(!empty($info['jid'])){
			$info['jd_time_type'] = date('Y-m-d H:i:s',$info['jdtime']);
			$info['shangname'] = $member->where("id = ".$info['sid'])->getField("account");
			$info['jdname'] = $member->where("id = ".$info['jid'])->getField("account");
			$info['jdqq'] = $member->where("id = ".$info['jid'])->getField("qq");
			//订单状态
			if ($info['status'] == 1){	$info['zhuangtai'] = "等待接单";	}
			elseif ($info['status'] == 2){	$info['zhuangtai'] = "客服审核";	}
			elseif ($info['status'] == 3){	$info['zhuangtai'] = "任务进行中";	}
			elseif ($info['status'] == 4){	$info['zhuangtai'] = "申请放款";	}
			elseif ($info['status'] == 5){	$info['zhuangtai'] = "任务完成";	}
			elseif ($info['status'] == 6){	$info['zhuangtai'] = "纠纷中";	}
			elseif ($info['status'] == 7){	$info['zhuangtai'] = "拒绝放单";	}
			elseif ($info['status'] == 8){	$info['zhuangtai'] = "刷手取消";	}
		}
			
		$this->assign("info",$info);	
		$this->display();
	}	
	/*
	 * 商家我的任务
	 */
	public function mylists(){
		$renwu = new RenwuModel();
		$member = new MemberModel();
		$map = array();
		$map['status'] = array("egt",1);
		$map['sid'] = MID;
		/************筛选*****************/
// 		$filter = I('post.');
// 		if($filter['cdtype']){//搜索任务信息 根据  淘宝号  平台号  任务号
// 			if($filter['type'] == 1){
				
// 			}elseif ($filter['type'] == 2){
				
// 			}elseif ($filter['type'] == 3){
				
// 			}
// 		}
// 		if($filter['cdstatus'] && $filter['cdstatus'] != 0){//搜索任务类型
// 			$map['status'] = $filter['cdstatus'];
// 		}

		/************筛选*****************/
		C('LIST_ROWS',C('cfg_linerows'));
		$list = $this->_lists($renwu,$map,'createtime DESC');
		//list数据处理
		foreach ($list as &$v) {
			$v['time_type'] =date('Y-m-d H:i:s',$v['updatetime']);
			if($v['status'] == 0){$v['zhuangtai'] = "任务取消";}
			if($v['status'] == 1){$v['zhuangtai'] = "等待接单";}
			if($v['status'] == 2){$v['zhuangtai'] = "审核刷手";}
			if($v['status'] == 3){$v['zhuangtai'] = "任务进行";}
			if($v['status'] == 4){$v['zhuangtai'] = "申请放款";}
			if($v['status'] == 5){$v['zhuangtai'] = "任务完结";}
			if($v['status'] == 6){$v['zhuangtai'] = "纠纷中";}
			if($v['status'] == 7){$v['zhuangtai'] = "拒绝放单";}
			if($v['status'] == 8){$v['zhuangtai'] = "刷手取消";}
			if(!empty($v['jid'])){
				$v['qq'] = $member->where("id = ".$v['jid'])->getField("qq");
				$v['account'] = $member->where("id = ".$v['jid'])->getField("account");
			}						
		}
		unset($v);
		$this->assign("list",$list);
		$this->display();
	}
	/*
	 * 商家账户信息
	 */
	public function userinfo() {
		if(IS_POST){
			$member = new MemberModel();
			$data = I('post.');
			if($member->where("id = ".MID)->save($data)){
				$this->success("成功");
			}else {	$this->error("失败");}
		}else {
			//账户信息
			$member = new MemberModel();
			$this->info = $member->where("id = ".MID)->find();
			$this->display();
		}
	}
	/*
	 * 商家账户登录密码
	 */
	public function usermima() {
		if(IS_POST){
			$member = new MemberModel();
			$m = $member->where("id = ".MID)->getField("password");
			$data = I('post.');
			if(pwd_hash($data['ypassword']) != $m){
				$this->error("失败");
			}
			if($member->where("id = ".MID)->setField("password",pwd_hash($data['npassword']))){
				$this->success("成功");
			}			
		}else{
			//账户信息
			$member = new MemberModel();
			$this->info = $member->where("id = ".MID)->find();
			$this->display();
		}
	}
	/*
	 * 商家交易密码
	 */
	public function jiaoyimima() {
		if(IS_POST){
			$member = new MemberModel();
			$m = $member->where("id = ".MID)->getField("password");
			$data = I('post.');
			if(pwd_hash($data['dpassword']) != $m){
				$this->error("失败");
			}
			if($member->where("id = ".MID)->setField("jpassword",pwd_hash($data['jpassword']))){
				$this->success("成功");
			}			
		}else{
			//账户信息
			$member = new MemberModel();
			$this->info = $member->where("id = ".MID)->find();
			$this->display();
		}
	}
	/*
	 * 商家充值
	 */
	public function chongzhi() {
		$jilu = new JiluModel();
		if(IS_POST){
			$chongzhi = new ChongzhiModel();
			$data = I('post.');
			$map = array();
			$map['money'] = $data['money'];
			$map['sid'] = MID;
			$map['createtime'] = time();
			if($chongzhi->add($map)){
				$this->success("充值申请提价成功！");
			}else{
				$this->success("充值申请提价成功！");
			}
		}else{
			//充值信息
			$list = $jilu->where("cate = 1 and sid = ".MID)->select();
			//list数据处理
			foreach ($list as &$v) {
				$v['time_type'] =date('Y-m-d H:i:s',$v['createtime']);
			}
			unset($v);
			$this->assign("list",$list);
			//充值码
			$this->id = MID;
			$this->display();
		}
	}
	/*
	 * 商家体现
	 * 1、可用余额减少对应数值
	 * 2、冻结金额增加对应数值
	 * 3、增加体现记录
	 */
	public function tixian() {
		$jilu = new JiluModel();
		if(IS_POST){
			$member = new MemberModel();
			$jilu = new JiluModel();
			$m = $member->where("id = ".MID)->getField("jpassword");
			$data = I('post.');
			if(pwd_hash($data['jpassword']) != $m){
				$this->error("交易密码错误");
			}
						
			$model = new Model();
			$model->startTrans();
			
			if ($data['money'] > $member->where("id = ".MID)->getField("able_money")){//判断提现金额是否大约可用余额
				$model->rollback();
				$this->error("提现金额大约可用余额！");
			}
			
			if(false === $member->where("id = ".MID)->setDec("able_money",$data['money'])){//减少可用余额
				$model->rollback();
				$this->error("减少可用余额失败！");
			}
			
			if(false === $member->where("id = ".MID)->setInc("ice_money",$data['money'])){//增加冻结金额
				$model->rollback();
				$this->error("增加冻结金额失败！");
			}
			
			$map = array();
			$map['sid'] = MID;
			$map['money'] = $data['money'];
			$map['xingming'] = $data['realname'];
			$map['zhifubao'] = $data['zhifubao'];
			$map['createtime'] = time();
			$map['cate'] = 2;
			$map['status'] = 0;
			$map['remark'] = "用户提现";
			$map['yue'] = $member->where("id = ".MID)->getField("able_money");
			if(false === $jilu->add($map)){//添加提现记录
				$model->rollback();
				$this->error("增加提现记录失败！");
			}	
			
			$model->commit();
			$this->success("体现申请提交成功！");
		}else{
			//提现信息
			$list = $jilu->where("cate = 2 and sid = ".MID)->select();
			//list数据处理
			foreach ($list as &$v) {
				$v['time_type'] =date('Y-m-d H:i:s',$v['createtime']);
			}
			unset($v);
			$this->assign("list",$list);
			//账户信息
			$member = new MemberModel();
			$this->info = $member->where("id = ".MID)->find();
			$this->display();
		}
	}
	/*
	 * 商家个人资金
	*/
	public function userzijin() {
		$jilu = new JiluModel();
		//资金明细
		$zjlist = $jilu->where("sid = ".MID)->select();
		//list数据处理
		foreach ($zjlist as &$v) {
			$v['time_type'] =date('Y-m-d H:i:s',$v['createtime']);
		}
		unset($v);
		$this->assign("zjlist",$zjlist);
		$this->display();
	}	
	/*
	 * 商家发布任务
	*/
	public function fabu($mbid = NULL){	
		$renwu = new RenwuModel();
		if(IS_POST){
			$data = I('post.');
			$money = $data['price'];//任务总价
			$data['yongjin'] = $data['yj'];//任务佣金
			$number = $data['number'];//发布数量
			$data['kouchu'] = ($money + $data['yongjin'] + 1) * $number;

			if($data['kouchu'] > $member->where("id = ".MID)->getField("able_money")){
				$this->error("帐户可用余额不足，请充值！",U('Shang/chongzhi'));
			}
			
			//任务表添加数据
			$data['sid'] = MID;
			$data['createtime'] = time();
			$data['updatetime'] = time();
			$data['picurl'] = $_FILES['thumb'];
			if (empty($_FILES['thumb']) || $data['picurl']['error']=='4') {
				unset($data['picurl']);
			}
			
			//发布扣费  更改账户总额  更改可用余额
			$model = new Model();
			$jilu = new JiluModel();
			
			if (false === $renwu->myAddAll($data,$number)) {//任务表添加数据
				$this->error("发布失败！");
			}else {
				$tid = $member->where("id = ".MID)->getField("parent_id");//当前登录用户信息
				if(!empty($tid)){//判断当前发布任务的用户是否有推荐人
					$tinfo = $member->where("id = ".$tid)->find();
					$model = new Model();
					$jilu = new JiluModel();
					$model->startTrans();
					
					$xian = strval(1 * ((float)$tinfo['bili']/100));	
					if(false == $member->where("id = ".$tinfo['id'])->setInc("able_money",$xian)){//更改可用余额
						$model->rollback();
// 						$this->error('更新失败,未知错误!');
					}
						
					if (false == $member->where("id = ".$tinfo['id'])->setInc("total_money",$xian)){//更改总余额
						$model->rollback();
// 						$this->error('更新失败,未知错误!');
					}
						
					$tjdata = array();
					$tjdata['money'] = $xian;
					$tjdata['remark'] = '被推荐商家发布任务返现';
					$tjdata['createtime'] = time();
					$tjdata['cate'] = 7;
					$tjdata['status'] = 1;
					$tjdata['sid'] = $tinfo['id'];
					$tjdata['yue'] = $member->where("id = ".$tinfo['id'])->getField("total_money");
					if (false == $jilu->add($tjdata)){//添加推荐注册返现记录
						$model->rollback();
// 						$this->error('更新失败,未知错误!');
					}
					$model->commit();
				}
				$this->success("发布成功");
			}
		}else{
			//用户储存的模板
			$map = array();$map['sid'] = MID;$map['status'] = array("gt",-1);$map['moban'] = array("neq","");
			$list = $renwu->where($map)->select();
			//list数据处理
			foreach ($list as &$v) {	$v['time_type'] =date('Y-m-d H:i:s',$v['createtime']);	}	unset($v);
			$this->assign("list",$list);
			//选中的模板数据
			if($mbid){	$this->info = $renwu->where("id = ".$mbid)->find();	}
			//商家绑定的袋内普
			$dianpu = new DianpuModel();
			$this->dianpu = $dianpu->where("sid = ".MID)->select();
			$this->display();
		}
	}
	/*
	 * 商家模板管理
	*/
	public function moban(){
		$renwu = new RenwuModel();
		$map = array();
		$map['sid'] = MID;
		$map['status'] = array("gt","");
		$map['moban'] = array("neq","");
		$list = $renwu->where($map)->select();
		//list数据处理
		foreach ($list as &$v) {
			$v['time_type'] =date('Y-m-d H:i:s',$v['createtime']);
		}
		unset($v);
		$this->assign("list",$list);
		$this->display();
	}
	/*
	 * 商家删除模板
	*/
	public function del($id){
		$renwu = new RenwuModel();
		if(false == $renwu->where("id = ".$id)->setField("moban","")){
			$this->error("失败");
		}else{
			$this->success("成功");
		}
	}
	/*
	 * 更改做任务状态
	 */
	public function upsta($flag,$id){
		$renwu = new RenwuModel();
		$member = new MemberModel();
		//判断商家是否取消任务
		if($flag == 0){//退还账户
			if(false == $renwu->renwuQuxiao($id)){
				$this->error("操作失败");
			}else {
				$this->success("操作成功");
			}
		}elseif ($flag == 4){
			$jid = $renwu->where("id = ".$id)->getField("jid");//接单人id
			$tid = $member->where("id = ".$jid)->getField("parent_id");//接单人信息
			if(!empty($tid)){//如果接单用户有推荐人
				if(false == $renwu->renwuFangkuan($tid,$id)){
					$this->error("操作失败");
				}else {
					$this->success("操作成功");
				}
			}else {
				if(false == $renwu->where("id=".$id)->setField("status",$flag)){
					$this->error("操作失败");
				}else{
					$this->success("操作成功");
				}
			}	
		}else{
			if(false == $renwu->where("id=".$id)->setField("status",$flag)){
				$this->error("操作失败");
			}else{
				$this->success("操作成功");
			}
		}	
	}
	/*
	 * 商家绑定店铺跑
	 */
	public function dianpu(){
		$dianpu = new DianpuModel();
		if(IS_POST){
			$data = array();
			$data['name'] = I('post.name');
			$data['zhanggui'] = I('post.zhanggui');
			$data['name'] = I('post.name');
			
			if (empty($_FILES['shouye']) || $data['shouye']['error']=='4') {
				unset($data['shouye']);
			}
			if (empty($_FILES['houtai']) || $data['houtai']['error']=='4') {
				unset($data['houtai']);
			}
			if (false === $dianpu->myBangding($data)) {
				$this->error("提交失败");
			}else {
				$this->success("提交成功",U('Shang/dianpu'));
			}
		}else{
			$this->list = $dianpu->where("sid = ".MID)->select();
			$this->display();
		}		
	}
	/*
	 * 商家实名认证
	 */
	public function renzheng(){
		$member = new MemberModel();
		$model = new Model();
		$jilu = new JiluModel();
		if(IS_POST){
			$data = array();
			$data['id'] = MID;
			$data['realname'] = I('post.realname');
			$data['idcard'] = I('post.cardid');
			$data['zhengmian'] = $_FILES['zhengmian'];
			$data['beimian'] = $_FILES['beimian'];
	
			if (empty($_FILES['zhengmian']) || $data['zhengmian']['error']=='4') {
				unset($data['zhengmian']);
			}
			if (empty($_FILES['beimian']) || $data['beimian']['error']=='4') {
				unset($data['beimian']);
			}
			if (false === $member->myRenzheng($data)) {
				$this->error("提交失败");
			}else {
				$this->success("提交成功",U('Shua/index'));
			}
		}else {
			$this->info=$member->where("id=".MID)->find();
			$this->display();
		}
	}
}